package nl.infodation.emr.pricingservice.ui;

import java.util.List;

import nl.infodation.emr.pricingservice.controller.ControllerFactory;
import nl.infodation.emr.pricingservice.ui.component.ExplicitPagingContainer;
import nl.infodation.emr.pricingservice.ui.page.SessionData;
import nl.infodation.emr.utility.Constants;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Table;

@SuppressWarnings("rawtypes")
public class ProductSelectionList extends Table {
	
	private static final long serialVersionUID = 1L;
	final private ExplicitPagingContainer pagingContainer=new ExplicitPagingContainer();
	
	public Object[] STANDARD_COL_NAME;
	
	public String[] STANDARD_COL_HEADER;
	
	private void initColumnData() {
		STANDARD_COL_NAME = new Object[] {
				"countryCode",
				"languageCode",
				"productCode",
				"productId",
				"refcoId",
				"itemId",
			};
		
		STANDARD_COL_HEADER = new String[] {
				SessionData.getI18N().getMessage(Constants.COUNTRY_HEADER),
				SessionData.getI18N().getMessage(Constants.LANGUAGE_HEADER),
				SessionData.getI18N().getMessage(Constants.PRODUCT_CODE_HEADER),
				SessionData.getI18N().getMessage(Constants.PRODUCT_ID_HEADER),
				SessionData.getI18N().getMessage(Constants.REFCO_ID_HEADER),
				SessionData.getI18N().getMessage(Constants.ITEM_ID_HEADER),
			};
	}
	
	public ProductSelectionList(){
		initColumnData();
		setSizeFull();
		initializePagingContainer();
		setContainerDataSource(pagingContainer);
		setVisibleColumns(STANDARD_COL_NAME);
        setColumnHeaders(STANDARD_COL_HEADER);
        setColumnCollapsingAllowed(true);
        setColumnReorderingAllowed(true);
        setSelectable(true);
        setImmediate(true);
        setNullSelectionAllowed(false);
	}
	
	private void initializePagingContainer() {
		pagingContainer.addContainerProperty("countryCode", String.class, null);
		pagingContainer.addContainerProperty("languageCode", String.class, null);
		pagingContainer.addContainerProperty("productCode", String.class, null);
		pagingContainer.addContainerProperty("productId", int.class, null);
		pagingContainer.addContainerProperty("refcoId", String.class, null);
		pagingContainer.addContainerProperty("itemId", long.class, null);
	}

	public void loadPage(int page) {
		List<BeanItem> items = ControllerFactory
				.getProductSelectionController()
				.getPaginationList(page,
						pagingContainer.getPageSize());
		pagingContainer.setDataSource(items);
	}
	
	public void reload(){
		loadPage(1);
	}
	
	public void setDataSource(final List<BeanItem> beans) {
		pagingContainer.setDataSource(beans);
	}
	
	public int getPageSize()
	{
		return pagingContainer.getPageSize();
	}
}
